package com.kinon.ceshiaccount.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kinon.ceshiaccount.R;
import com.kinon.ceshiaccount.bean.base.BaseDataBean;
import com.kinon.ceshiaccount.bean.base.OrdersBean;
import com.kinon.ceshiaccount.network.HttpManage;
import com.kinon.ceshiaccount.util.SPManage;
import com.kinon.ceshiaccount.view.CustomDialog;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.kinon.ceshiaccount.R.id.tv_orderRefunds;

/**
 * Created by luohao on 2017-07-19.
 * 记录中的订单适配器
 */

public class RecordAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<OrdersBean> list;

    public RecordAdapter(Context context, ArrayList<OrdersBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
//        Log.e("lh","getView---");
        final ViewHandler handler;
        if(view==null){
            handler=new ViewHandler();
            view= LayoutInflater.from(context).inflate(R.layout.item_record_layout,null);
            handler.tv_orderId= (TextView) view.findViewById(R.id.tv_orderId);
            handler.tv_orderName= (TextView) view.findViewById(R.id.tv_orderName);
            handler.tv_orderMoney= (TextView) view.findViewById(R.id.tv_orderMoney);
            handler.tv_orderTime= (TextView) view.findViewById(R.id.tv_orderTime);
            handler.tv_orderRefunds= (TextView) view.findViewById(tv_orderRefunds);
            view.setTag(handler);
        }else{
            handler= (ViewHandler) view.getTag();
        }
        handler.tv_orderId.setText(list.get(i).getOrderno());
        handler.tv_orderName.setText(list.get(i).getName());
        handler.tv_orderMoney.setText(list.get(i).getTotal());
        handler.tv_orderTime.setText(list.get(i).getOrder_time());

        int refund=list.get(i).getRefund();
        if(refund==0){
            handler.tv_orderRefunds.setText(R.string.refund_proces);
            handler.tv_orderRefunds.setClickable(true);
        }else if(refund==1){
            handler.tv_orderRefunds.setText(R.string.refund_processing);
            handler.tv_orderRefunds.setClickable(false);
        }else {
            handler.tv_orderRefunds.setText(R.string.refund_complete);
            handler.tv_orderRefunds.setClickable(false);
        }
        handler.tv_orderRefunds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog(handler.tv_orderRefunds,i);
            }
        });
        return view;
    }
    private class ViewHandler{
        private TextView tv_orderId;
        private TextView tv_orderName;
        private TextView tv_orderMoney;
        private TextView tv_orderTime;
        private TextView tv_orderRefunds;
    }
    private void dialog(final  TextView tv_orderRefunds,final int position){
        CustomDialog dialog=new CustomDialog(context, null, "请确认是否退款", null, null,
                new CustomDialog.DialogClickListener() {
            @Override
            public void onDialogClick(int btn) {
                if(btn==1){
                    refund(tv_orderRefunds,position);
                }
            }
        });
        dialog.show();
    }

    private void refund(final  TextView tv_orderRefunds,final int position){
        Call<BaseDataBean> call= HttpManage.getRequestApi().refund
                (SPManage.getInstance().getToken(),list.get(position).getId());
        call.enqueue(new Callback<BaseDataBean>() {
            @Override
            public void onResponse(Call<BaseDataBean> call, Response<BaseDataBean> response) {
                if(response.body().isCode()){
                    list.get(position).setRefund(1);
                    tv_orderRefunds.setText(R.string.refund_complete);
                    tv_orderRefunds.setClickable(false);
                }
            }

            @Override
            public void onFailure(Call<BaseDataBean> call, Throwable t) {

            }
        });

    }
}
